import {
  Column,
  Entity,
  JoinTable,
  ManyToMany,
  PrimaryGeneratedColumn
} from 'typeorm'

import { SysDeptEntity, SysMenuEntity, SysUserEntity } from '.'
import { BaseEntity } from './base.entity'

@Entity()
export class SysRoleEntity extends BaseEntity implements ISysRoleEntity {
  @PrimaryGeneratedColumn({ type: 'bigint' })
  roleId: string

  @Column()
  roleName: string

  @Column()
  roleKey: string

  @Column()
  roleSort: number

  @Column()
  status: string

  @Column()
  delFlag: string

  @Column()
  remark: string

  @ManyToMany(type => SysUserEntity, user => user.roles)
  users: SysUserEntity[]

  @ManyToMany(type => SysDeptEntity, dept => dept.roles)
  @JoinTable({
    name: 'sys_role_dept',
    joinColumn: {
      name: 'role_id'
    },
    inverseJoinColumn: {
      name: 'dept_id'
    }
  })
  depts: SysDeptEntity[]

  @ManyToMany(type => SysMenuEntity, menu => menu.roles)
  @JoinTable({
    name: 'sys_role_menu',
    joinColumn: {
      name: 'role_id'
    },
    inverseJoinColumn: {
      name: 'menu_id'
    }
  })
  menus: SysMenuEntity[]
}
